# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


from typing import Optional


class Solution:
    def pairSum(self, head: Optional[ListNode]) -> int:
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        half = slow

        pre = None
        cur = half
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt

        ret = 0
        while pre:
            ret = max(head.val + pre.val, ret)
            pre = pre.next
            head = head.next
        return ret
